package day240512.e1_interface;

public class Student implements MyComparable {
    String name;
    int age;

    public Student(String name, int age) {
        this.name = name;
        this.age = age;
    }

    @Override
    public int compareTo(MyComparable object) {
        if (object instanceof Student student) {
            if (this.age == student.age) return 0;
            return (this.age - student.age > 0) ? 1 : -1;
        } else {
            throw new ClassCastException("比较类型不匹配");
        }
    }

    @Override
    public String toString() {
        return this.name + "(" + this.age + ")";
    }
}
